package com.back.campus_back.controller;
import com.back.campus_back.domain.Admin;
import com.back.campus_back.service.AdminService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-04-17
 */
@RestController
@RequestMapping("/admin")
@Api(tags = "管理员的控制类")
public class AdminController {

    @Autowired
    private AdminService adminService;


//    @PostMapping("/login")
//    public Admin login(@Param("adminName")String adminName, @Param("adminPassword")String adminPassword){
//        return adminService.login(adminName,adminPassword);
//    }

    @RequestMapping(value = "/login/{adminName}/{adminPassword}", method = RequestMethod.POST)
    @CrossOrigin
    public Map<String, Object> login(@PathVariable("adminName") String adminName,@PathVariable("adminPassword") String adminPassword,

                                     HttpSession session,
                                     HttpServletResponse response) throws Exception {

//        String adminName=jsonParam.opt("adminName").toString();
//        String adminPassword = jsonParam.get("adminPassword").toString();
        Map<String, Object> map = new HashMap<String, Object>();
        Admin existAdmin = adminService.findByName(adminName);
        if (existAdmin == null) {
            map.put("msg", "该用户未注册");
            map.put("code",500);

            return map;
        }
        if (!existAdmin.getAdminPassword().equalsIgnoreCase(adminPassword)) {
            map.put("msg", "密码错误,请重新输入");
            map.put("code",500);
            return map;
        }
        session.setAttribute("loginAdmin", existAdmin);
        map.put("msg", "登录成功");
        map.put("code",200);
        return map;

//    @PostMapping(value = "/login")
//    @ApiOperation("管理员登录")
//    public Admin login(@RequestParam("admin_name")String admin_name,@RequestParam("admin_password")String admin_password){
//        return adminService.login(admin_name,admin_password);

    }


    @GetMapping("/findByName/{adminName}")
    public Admin findByName(@PathVariable("adminName") String adminName){
        Admin byName = adminService.findByName(adminName);
        return byName;

    }




}

